//
//  UISearchBar+Lc.m
//  18Birdies
//
//  Created by Lc on 15/5/8.
//  Copyright (c) 2015年 Lc. All rights reserved.
//

#import "LcFont.h"
#import "LcPreconditions.h"
#import "UIButton+Lc.h"
#import "UIColor+Lc.h"
#import "UISearchBar+Lc.h"

@implementation UISearchBar (Lc)

- (void)setSearchBarBackgroundColor:(UIColor*)color {
  UIView* searchBarBackgroundInView = [self findSearchBarBackgroundInView:self];
#ifdef DEBUG
  LC_CHECK_NOT_NULL(searchBarBackgroundInView);
#endif
  if (searchBarBackgroundInView != nil) {
    [searchBarBackgroundInView removeFromSuperview];
    self.backgroundColor = color;
  }
}

- (UIView*)findSearchBarBackgroundInView:(UIView*)view {
  if ([NSStringFromClass(view.class)
          isEqualToString:@"UISearchBarBackground"]) {
    return view;
  }
  if (view.subviews.count > 0) {
    for (UIView* subview in view.subviews) {
      UIView* result = [self findSearchBarBackgroundInView:subview];
      if (result != nil) {
        return result;
      }
    }
  }
  return nil;
}

- (void)setButtonFont:(LcFont*)lcFont {
  LC_CHECK_NOT_NULL(lcFont);
  [self setButtonFont:lcFont inView:self];
}

- (void)setButtonFont:(LcFont*)lcFont inView:(UIView*)view {
  if ([view isKindOfClass:UIButton.class]) {
    UIButton* button = (UIButton*)view;
    [button setLcFont:lcFont];
  }
  for (UIView* subview in view.subviews) {
    [self setButtonFont:lcFont inView:subview];
  }
}

- (void)enableButtons {
  [self enableButtonsInView:self];
}

- (void)enableButtonsInView:(UIView*)view {
  if ([view isKindOfClass:UIButton.class]) {
    UIButton* button = (UIButton*)view;
    button.enabled = YES;
  }
  for (UIView* subview in view.subviews) {
    [self enableButtonsInView:subview];
  }
}

@end
